package com.sita.linboard;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.sita.linboard.base.BaseActivity;
import com.sita.linboard.base.BaseApplication;
import com.sita.linboard.calllist.GetOrderRequest;
import com.sita.linboard.calllist.UtilsBean;
import com.sita.linboard.calllist.VehicleListBackBean;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.journey.OrderReceivActivity;
import com.sita.linboard.utils.SpUtils;
import com.sita.linboard.utils.ToastUtils;

import de.greenrobot.event.EventBus;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/9/14.
 */
public class RemindOrderActivity extends BaseActivity {
    private  String TAG = this.getClass().getSimpleName();
    private  CmdOrderBean cmdorderbean = new CmdOrderBean();
    private TextView time,startlocation,endlocation;
    private Button getOrder,cancel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        cmdorderbean = (CmdOrderBean) getIntent().getSerializableExtra("Cmd_Order");
        OrderonClick();
        time.setText(cmdorderbean.getTime());
        startlocation.setText(cmdorderbean.getDepartureaddr());
        endlocation.setText(cmdorderbean.getArrivaladdr());
        overridePendingTransition(R.anim.fadein,0);
    }

    private void OrderonClick(){
        getOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GetOrderRequest request = new GetOrderRequest();

                request.driver_id = SpUtils.getString("AccoundID", null, BaseApplication.getContext());

                request.trip_id =  cmdorderbean.getTripID();

                RestClient.getRestService().getOrder(request, new Callback<VehicleListBackBean>() {
                    @Override
                    public void success(VehicleListBackBean getOrderBackBean, Response response) {

                        if (getOrderBackBean.getErrorCode().equals("0")&&getOrderBackBean.getData().size()!=0) {

                             if(getOrderBackBean.getData().get(0).getState() == 1) {
                                //成功之后给发送透传
                                //由服务端发
                                // sendCMDMessage(bean);
                                UtilsBean utilsBean = new UtilsBean();
                                utilsBean.setArrival_addr(getOrderBackBean.getData().get(0).getArrival_addr());
                                utilsBean.setArrival_lat(getOrderBackBean.getData().get(0).getArrival_lat());
                                utilsBean.setArrival_lng(getOrderBackBean.getData().get(0).getArrival_lng());
                                utilsBean.setCustomer_id(getOrderBackBean.getData().get(0).getCustomer().getAccountId());
                                utilsBean.setDeparture_addr(getOrderBackBean.getData().get(0).getDeparture_addr());
                                utilsBean.setDeparture_lng(getOrderBackBean.getData().get(0).getDeparture_lng());
                                utilsBean.setDeparture_lat(getOrderBackBean.getData().get(0).getDeparture_lat());
                                utilsBean.setTrip_id(getOrderBackBean.getData().get(0).getTrip_id());
                                if (getOrderBackBean!=null){
                                    OrderReceivActivity.OrderIntent(RemindOrderActivity.this, utilsBean, "0");
                                }
                                SpUtils.putString("trip_id", getOrderBackBean.getData().get(0).getTrip_id(), BaseApplication.getContext());
                                SpUtils.putString("user_poone",getOrderBackBean.getData().get(0).getCustomer().getMobile(),BaseApplication.getContext());
                            } else {
                                ToastUtils.show("抢单失败",4);

                            }
                        }
                        else {
                                ToastUtils.show("乘客已取消订单",2);
                        }
                        finish();
                        overridePendingTransition(0,R.anim.fadeout);
                    }
                    @Override
                    public void failure(RetrofitError error) {
                        Log.e(TAG, error.toString());
                        ToastUtils.show("抢单失败",4);
                        finish();
                        overridePendingTransition(0,R.anim.fadeout);
                    }

                });
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              finish();
                overridePendingTransition(0,R.anim.fadeout);
            }
        });
    }
    @Override
    protected void initData() {
      //  OrderonClick();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        time = bindView(R.id.now_time);
        startlocation = bindView(R.id.start_user);
        endlocation = bindView(R.id.end_user);
        getOrder = bindView(R.id.getOrder);
        cancel = bindView(R.id.cancle);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected int getId() {
        return R.layout.activity_remindorder;
    }
    public static void RemindOrderIntent(Context context,CmdOrderBean bead){
        Intent intent = new Intent(context,RemindOrderActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("Cmd_Order",bead);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}
